<?php
namespace app\common\service;

use think\Model;

class TeachingStaffService extends BaseService
{


    /**
     * 微信端 通过班级ID获取教务人员列表
     * @param $sid
     * @param $classid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getListByClassid($sid, $classid){
        $field = 'u.user_id,u.user_nickname,u.user_name,u.user_headimgurl,u.user_mobile,tl.id as teaching_level_id,tl.name as teaching_title';
        $where = [
            'ts.sid' => $sid,
            'ts.classid' => $classid,
            'ts.status' => 1,
            'tl.status' => 1,
            'ui.counselor_status' => 1 //是顾问状态
        ];
        $list = db('teaching_staff ts')
            ->field($field)
            ->join('user_integral ui', 'ui.userid = ts.uid')
            ->join('user u', 'u.user_id = ui.userid')
            ->join('teaching_level tl', 'ts.teaching_level_id = tl.id')
            ->where($where)
            ->order('tl.sorting ASC')
            ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['user_name'] = $v['user_name'] ?? $v['user_nickname'];
                unset($list[$k]['user_nickname']);
            }
        }
        return $list;
    }

    /**
     * 后台 通过班级ID获取教务人员列表
     * @param $sid
     * @param $classid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getTeachingStaffListByClassid($sid, $classid){
        $level_list = db('teaching_level')
                        ->field('id as teaching_level_id,name as title_name')
                        ->where(['sid' => $sid, 'status' => 1])
                        ->order('sorting ASC')
                        ->select();

        if($level_list){
            foreach($level_list as $k => $v){
                $field = 'u.user_id as uid,u.user_nickname,u.user_name,u.user_headimgurl,u.user_mobile';
                $where = [
                    'ts.sid' => $sid,
                    'ts.classid' => $classid,
                    'ts.status' => 1,
                    'ts.teaching_level_id' => $v['teaching_level_id'],
                    'ui.counselor_status' => 1 //是顾问状态
                ];
                $teaching_staff = db('teaching_staff ts')
                                ->field($field)
                                ->join('user_integral ui', 'ui.userid = ts.uid')
                                ->join('user u', 'u.user_id = ui.userid')
                                ->where($where)
                                ->find();
                if($teaching_staff){
                    $level_list[$k] = array_merge($v, $teaching_staff);
                }
            }
        }
        return $level_list;
    }


    /**
     * 后台: 设为教务
     * @param $sid
     * @param $uid
     * @param $class_id
     * @param $teaching_level_id
     * @return array
     */
    public function becomeTeachingStaff($sid, $uid, $class_id, $teaching_level_id){
        $where = [
            'sid' => $sid,
            'classid' => $class_id,
            'teaching_level_id' => $teaching_level_id,
            'status' => 1
        ];
        $info = db('teaching_staff')->where($where)->find();
        if($info){
            $r = db('teaching_staff')
                ->where($where)
                ->update(['edit_time' => time(), 'uid' => $uid]);
        } else {
            $data = [
                'uid' => $uid,
                'sid' => $sid,
                'classid' => $class_id,
                'teaching_level_id' => $teaching_level_id,
                'add_time' => time()
            ];
            $r = db('teaching_staff')->insert($data);
        }
        return $r===false ? ['status' => 0, 'msg' => '设置失败'] : ['status' => 1, 'msg' => '设置成功'];
    }


    /**
     * 后台: 取消教务头衔
     * @param $sid
     * @param $uid
     * @param $class_id
     * @param $teaching_level_id
     * @return array
     */
    public function cancelTeachingStaff($sid, $uid, $class_id, $teaching_level_id){
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'class_id' => $class_id,
            'teaching_level_id' => $teaching_level_id
        ];
        $r = db('teaching_staff')->where($where)->setField('teaching_level_id', 0);
        return $r ? ['status' => 1, 'msg' => '取消成功'] : ['status' => 0, 'msg' => '取消失败'];
    }
}







